package com.ficus.road.maintain.service;

import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.core.model.xlj.Xlj;
import com.ficus.road.maintain.core.model.zh.xlj.XljLatestInfo;
import com.ficus.road.maintain.core.model.zh.xlj.XljWarning;
import com.ficus.road.maintain.core.model.zh.xlj.XljWarningFlow;
import com.ficus.road.maintain.dao.HitDao;
import com.ficus.road.maintain.dao.xlj.XljDao;
import com.ficus.road.maintain.dao.xlj.XljWarningDao;
import com.ficus.road.maintain.dao.xlj.XljWaterLevelDao;
import com.ficus.road.maintain.dao.xlj.XljWaterLevelHistoryDao;
import com.ficus.road.maintain.util.MyDateUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2021/8/13 上午11:36
 */
@Service
@Slf4j
public class EmergencyService {

    private static final SimpleDateFormat ySimpleDateFormat = new SimpleDateFormat("yyyy");
    private static final SimpleDateFormat yMdSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat yMdHmsSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    private XljDao xljDao;
    @Autowired
    private XljWaterLevelDao waterLevelDao;
    @Autowired
    private XljWaterLevelHistoryDao waterLevelHistoryDao;
    @Autowired
    private XljWarningDao xljWarningDao;
    @Autowired
    private HitDao hitDao;

    public PageInfo<XljVO> queryXljList(int start, int limit, XljQueryRequest xljQueryRequest) {
        PageInfo<XljVO> pageResult = new PageInfo<>();
        List<XljVO> xljVoArrayList = new ArrayList<>();
        List<Xlj> xljDOList = xljDao.findAll();
        Query query = new Query();
        if (ObjectUtils.isNotEmpty(xljQueryRequest)) {
            query.addCriteria(Criteria.where("create_time").gte(xljQueryRequest.getStartTime()).lte(xljQueryRequest.getEndTime()));
        }
        Map<String, List<XljWarning>> xljIdWarningMap =
                xljWarningDao.findByQuery(query)
                        .stream()
                        .collect(Collectors.groupingBy(XljWarning::getXljId));
        Date startOfDay = MyDateUtil.getStartOfDay(new Date());
        xljDOList.forEach(item -> {
            XljLatestInfo xljLatestInfo = item.getXljLatestInfo();
            boolean latestInfoIsNull = xljLatestInfo == null;
            Query warningQuery = new Query(Criteria.where("xlj_id").is(item.getUniIdHuwan()));
            if (ObjectUtils.isNotEmpty(xljQueryRequest)) {
                warningQuery.addCriteria(Criteria.where("create_time").gte(xljQueryRequest.getStartTime()).lte(xljQueryRequest.getEndTime()));
            }
            XljWarning latestXljWarning =
                    xljWarningDao.findOne(warningQuery.with(new Sort(Sort.Direction.DESC, "create_time")).skip(0).limit(1));
            String warningPicUrl = null, closedPicUrl = null, unclosedPicUrl = null, finishPicUrl = null;
            Date warningTime = null, closedTime = null, unClosedTime = null, finishTime = null,
                    sendTraPoliceTime = null, sendWatServTime = null, sendManCentTime = null, messageTime = null;
            String warningInfo = null, warningRemark = null, closedInfo = null, closedRemark = null,
                    unclosedInfo = null, unclosedRemark = null, finishInfo = null, finishRemark = null;
            String rcvTime = "无预警";
            if (ObjectUtils.isNotEmpty(latestXljWarning)) {
                XljWarningFlow flow = latestXljWarning.getXljWarningFlow();
                rcvTime = MyDateUtil.dateFormatStr(latestXljWarning.getMaxDepthTime(), "yyyy-MM-dd HH:mm:ss");
                if (ObjectUtils.isNotEmpty(flow)) {
                    sendTraPoliceTime = flow.getSendTraPolice();
                    sendWatServTime = flow.getSendWatServ();
                    sendManCentTime = flow.getSendManCent();
                    messageTime = flow.getMessageTime();

                    List<XljWarningFlow.XljStatus> statusList = flow.getXljStatusList();
                    if (CollectionUtils.isNotEmpty(statusList)) {
                        Optional<XljWarningFlow.XljStatus> warningStatus = statusList.stream().filter(status -> status.getXljCondition().equals(1)).findFirst();
                        warningTime = warningStatus.map(XljWarningFlow.XljStatus::getCondTime).orElse(null);
                        warningPicUrl = warningStatus.map(XljWarningFlow.XljStatus::getCondImgUrl).orElse(null);
                        Optional<XljWarningFlow.XljStatus> closedStatus = statusList.stream().filter(status -> status.getXljCondition().equals(3)).findFirst();
                        closedTime = closedStatus.map(XljWarningFlow.XljStatus::getCondTime).orElse(null);
                        closedPicUrl = closedStatus.map(XljWarningFlow.XljStatus::getCondImgUrl).orElse(null);
                        Optional<XljWarningFlow.XljStatus> unclosedStatus = statusList.stream().filter(status -> status.getXljCondition().equals(4)).findFirst();
                        unClosedTime = unclosedStatus.map(XljWarningFlow.XljStatus::getCondTime).orElse(null);
                        unclosedPicUrl = unclosedStatus.map(XljWarningFlow.XljStatus::getCondImgUrl).orElse(null);
                        Optional<XljWarningFlow.XljStatus> finishStatus = statusList.stream().filter(status -> status.getXljCondition().equals(5)).findFirst();
                        finishTime = finishStatus.map(XljWarningFlow.XljStatus::getCondTime).orElse(null);
                        finishPicUrl = finishStatus.map(XljWarningFlow.XljStatus::getCondImgUrl).orElse(null);
                    }
                    List<XljWarningFlow.XljWarningInfo> xljWarningInfoList = flow.getXljWarningInfoList();
                    if (CollectionUtils.isNotEmpty(xljWarningInfoList)) {
                        Optional<XljWarningFlow.XljWarningInfo> warningDescription = xljWarningInfoList.stream()
                                .filter(info -> ObjectUtils.isNotEmpty(info.getINFO()))
                                .filter(des -> des.getINFO().contains("预警") || des.getREMARK().contains("预警")).findFirst();
                        warningInfo = warningDescription.map(XljWarningFlow.XljWarningInfo::getINFO).orElse(null);
                        warningRemark = warningDescription.map(XljWarningFlow.XljWarningInfo::getREMARK).orElse(null);
                        Optional<XljWarningFlow.XljWarningInfo> closedDescription = xljWarningInfoList.stream()
                                .filter(info -> ObjectUtils.isNotEmpty(info.getINFO()))
                                .filter(des -> des.getINFO().contains("下立交封交") || des.getREMARK().contains("下立交封交")).findFirst();
                        closedInfo = closedDescription.map(XljWarningFlow.XljWarningInfo::getINFO).orElse(null);
                        closedRemark = closedDescription.map(XljWarningFlow.XljWarningInfo::getREMARK).orElse(null);
                        Optional<XljWarningFlow.XljWarningInfo> unclosedDescription = xljWarningInfoList.stream()
                                .filter(info -> ObjectUtils.isNotEmpty(info.getINFO()))
                                .filter(des -> des.getINFO().contains("解除封交") || des.getREMARK().contains("解除封交")).findFirst();
                        unclosedInfo = unclosedDescription.map(XljWarningFlow.XljWarningInfo::getINFO).orElse(null);
                        unclosedRemark = unclosedDescription.map(XljWarningFlow.XljWarningInfo::getREMARK).orElse(null);
                        Optional<XljWarningFlow.XljWarningInfo> finishDescription = xljWarningInfoList.stream()
                                .filter(info -> ObjectUtils.isNotEmpty(info.getINFO()))
                                .filter(des -> des.getINFO().contains("预警结束") || des.getREMARK().contains("预警结束")).findFirst();
                        finishInfo = finishDescription.map(XljWarningFlow.XljWarningInfo::getINFO).orElse(null);
                        finishRemark = finishDescription.map(XljWarningFlow.XljWarningInfo::getREMARK).orElse(null);

                    }
                }
            }
            XljWarning warning = xljWarningDao.findOne(new Query(Criteria.where("xlj_id").is(item.getUniIdHuwan()).and("create_time").gte(startOfDay))
                    .with(new Sort(Sort.Direction.DESC, "create_time")).skip(0).limit(1));
            int isSign = 3;
            // 判定签到逻辑
            if (ObjectUtils.isNotEmpty(warning) && ObjectUtils.isNotEmpty(warning.getXljWarningFlow())) {
                List<XljWarningFlow.XljStatus> xljStatusList = warning.getXljWarningFlow().getXljStatusList();
                if (ObjectUtils.isNotEmpty(xljStatusList)) {
                    Optional<XljWarningFlow.XljStatus> any = xljStatusList.stream().filter(status -> status.getXljCondition().equals(2)).findAny();
                    if (any.isPresent()) {
                        XljWarningFlow.XljStatus xljStatus = any.get();
                        isSign = ObjectUtils.isEmpty(xljStatus.getCondTime()) ? 0 : 1;
                    }
                }
            }
            // 统计历史封交次数
            List<XljWarning> xljWarningList = xljIdWarningMap.getOrDefault(item.getUniIdHuwan(), new ArrayList<>());
            long closedTotalTimes = xljWarningList.stream()
                    .map(XljWarning::getXljWarningFlow)
                    .filter(ObjectUtils::isNotEmpty)
                    .flatMap(f -> f.getXljStatusList().stream())
                    .filter(t -> com.google.common.base.Objects.equal(3, t.getXljCondition()))
                    .count();
            XljVO xljVO = XljVO.builder()
                    .uniIdHuwan(item.getUniIdHuwan())
                    .xljName(item.getXljName())
                    .xljNature(item.getXljNature())
                    .manageUnit(item.getManageUnit())
                    .latitude(item.getLatitude())
                    .longitude(item.getLongitude())
                    .rcvTime(rcvTime)
                    .maintain(item.getMaintain())
                    .manageTel(item.getManageTel())
                    .waterTel(item.getWaterTel())
                    .waterUnit(item.getWaterUnit())
                    .police(item.getPolice())
                    .cameraInfo(item.getCameras())
                    .warnTotalTimes(xljIdWarningMap.getOrDefault(item.getUniIdHuwan(), new ArrayList<>()).size())
                    .closedTotalTimes((int) closedTotalTimes)
                    .waterLevel(latestInfoIsNull ? 0 : (xljLatestInfo.getValue() == null ? 0 : xljLatestInfo.getValue()))
                    .waterLevelTime(latestInfoIsNull ? null : xljLatestInfo.getUpdateTime())
                    .updateTime(latestInfoIsNull ? null : xljLatestInfo.getUpdateTime())
                    .isClosed(latestInfoIsNull ? 0 : xljLatestInfo.getCloseStatus())
                    .isPumping(latestInfoIsNull ? 0 : xljLatestInfo.getPumpingStatus())
                    .isSign(isSign)
                    .isGuard(latestInfoIsNull ? 0 : xljLatestInfo.getGuardStatus())
                    .state(latestInfoIsNull ? "在线" : xljLatestInfo.getState())
                    .warningTime(warningTime)
                    .warningPicUrl(warningPicUrl)
                    .warningInfo(warningInfo)
                    .warningRemark(warningRemark)
                    .closedTime(closedTime)
                    .closedPicUrl(closedPicUrl)
                    .closedInfo(closedInfo)
                    .closedRemark(closedRemark)
                    .unClosedTime(unClosedTime)
                    .unClosedPicUrl(unclosedPicUrl)
                    .unclosedInfo(unclosedInfo)
                    .unclosedRemark(unclosedRemark)
                    .finishTime(finishTime)
                    .finishPicUrl(finishPicUrl)
                    .finishInfo(finishInfo)
                    .finishRemark(finishRemark)
                    // 发送交警时间和发送水务时间替换成短信发送时间
                    .sendTraPoliceTime(messageTime)
                    .sendWatServTime(messageTime)
                    .sendManCentTime(sendManCentTime)
                    .messageTime(messageTime)
                    .build();
            xljVoArrayList.add(xljVO);
        });
        xljVoArrayList.sort(Comparator.comparing(XljVO::getXljNature).reversed());
        pageResult.setTotal(xljVoArrayList.size());
        if (xljVoArrayList.size() > start) {
            int end = start + limit;
            if (start + limit > xljVoArrayList.size()) {
                end = xljVoArrayList.size();
            }
            List<XljVO> fallDownVOS1 = xljVoArrayList.subList(start, end);
            pageResult.setList(fallDownVOS1);
        } else {
            pageResult.setList(new ArrayList<>());
        }
        return pageResult;
    }

    public XljStatisticVO queryXljStatistic(XljQueryRequest xljQueryRequest) {
        int xljCount = xljDao.findAll().size();
        Query query = new Query();
        if (ObjectUtils.isNotEmpty(xljQueryRequest.getStartTime()) && ObjectUtils.isNotEmpty(xljQueryRequest.getEndTime())) {
            query.addCriteria(Criteria.where("create_time").gte(xljQueryRequest.getStartTime()).lte(xljQueryRequest.getEndTime()));
        }
        List<com.ficus.road.maintain.core.model.zh.xlj.XljWarning> xljWarningList
                = xljWarningDao.findByQuery(query);
        int maxDepth = 0;
        OptionalInt max = xljWarningList.stream().mapToInt(com.ficus.road.maintain.core.model.zh.xlj.XljWarning::getMaxDepth).max();
        if (max.isPresent()) {
            maxDepth = max.getAsInt();
        }
        Date startOfDay = MyDateUtil.getStartOfDay(new Date());
        List<XljWarning> allXljWarningList = xljWarningDao.findAll();
        // 统计今日预警数量
        long todayWarningCount = allXljWarningList.stream().filter(item -> item.getMaxDepthTime().compareTo(startOfDay) > 0).count();
        Map<String, List<com.ficus.road.maintain.core.model.zh.xlj.XljWarning>> xljWarningMap = xljWarningList.stream().collect(Collectors.groupingBy(com.ficus.road.maintain.core.model.zh.xlj.XljWarning::getXljId));
        Collection<List<com.ficus.road.maintain.core.model.zh.xlj.XljWarning>> values = xljWarningMap.values();
        // 按下立交统计历史告警数量
        long count25 = values.stream().filter(item -> item.size() > 20).count();
        long count20 = values.stream().filter(item -> item.size() <= 20 && item.size() > 15).count();
        long count15 = values.stream().filter(item -> item.size() <= 15 && item.size() > 10).count();
        long count10 = values.stream().filter(item -> item.size() <= 10 && item.size() > 5).count();
        long count5 = values.stream().filter(item -> item.size() <= 5 && item.size() > 0).count();
        long count0 = xljCount - (count5 + count10 + count15 + count20 + count25);
        // 按下立交统计历史封交数量
        List<Integer> closedStatistic = values.stream().map(this::countClosedXlj).collect(Collectors.toList());
        long close1 = closedStatistic.stream().filter(item -> item == 1 || item == 2).count();
        long close2 = closedStatistic.stream().filter(item -> item > 2).count();
        long close0 = xljCount - (close1 + close2);
        return XljStatisticVO.builder()
                .xljCount(xljCount)
                .waterLevel(maxDepth)
                .warningCount(allXljWarningList.size())
                .todayWarningCount((int) todayWarningCount)
                .count0((int) count0)
                .count5((int) count5)
                .count10((int) count10)
                .count15((int) count15)
                .count20((int) count20)
                .count25((int) count25)
                .close0((int) close0)
                .close1((int) close1)
                .close2((int) close2)
                .build();
    }

    private Integer countClosedXlj(List<XljWarning> warningList) {
        long count = warningList.stream()
                .filter(Objects::nonNull)
                .map(XljWarning::getXljWarningFlow)
                .filter(Objects::nonNull)
                .flatMap(f -> f.getXljStatusList().stream())
                .filter(status -> status.getXljCondition().equals(3))
                .count();
        return (int) count;
    }

    public XljHistoryVO queryXljHistory(String year, String uniIdHuwan) {
        int yearInt = Integer.parseInt(year);
        Date startDate;
        Date endDate;
        try {
            startDate = ySimpleDateFormat.parse(String.valueOf(yearInt));
            endDate = ySimpleDateFormat.parse(String.valueOf(yearInt + 1));
        } catch (ParseException e) {
            throw new RuntimeException("parameter year format error" + e);
        }

        Xlj xlj = xljDao.findOne(new Query(Criteria.where("uni_id_huwan").is(uniIdHuwan)));
        Assert.notNull(xlj, "bad request, uniIdHuwan is invalid");
        Query xljWarningQuery = new Query(Criteria.where("create_time").gte(startDate).lte(endDate).and("xlj_id").is(xlj.getUniIdHuwan()));
        xljWarningQuery.with(new Sort(Sort.Direction.DESC, "create_time"));
        List<com.ficus.road.maintain.core.model.zh.xlj.XljWarning> xljWarningList = xljWarningDao.findByQuery(xljWarningQuery);
        List<WaterLevelAndTime> warnings = xljWarningList.stream().map(item -> WaterLevelAndTime.builder()
                .waterLevel(item.getMaxDepth())
                .time(item.getMaxDepthTime())
                .build())
                .sorted(Comparator.comparing(WaterLevelAndTime::getTime).reversed())
                .collect(Collectors.toList());
        long level0 = xljWarningList.stream().filter(item -> item.getMaxDepth() <= 5).count();
        long level5 = xljWarningList.stream().filter(item -> item.getMaxDepth() > 5 && item.getMaxDepth() <= 10).count();
        long level10 = xljWarningList.stream().filter(item -> item.getMaxDepth() > 10 && item.getMaxDepth() <= 15).count();
        long level15 = xljWarningList.stream().filter(item -> item.getMaxDepth() > 15 && item.getMaxDepth() <= 20).count();
        long level20 = xljWarningList.stream().filter(item -> item.getMaxDepth() > 20 && item.getMaxDepth() <= 25).count();
        long level25 = xljWarningList.stream().filter(item -> item.getMaxDepth() > 25).count();
        //历史积水统计
        XljStatisticGroup xljStatisticGroup = XljStatisticGroup.builder()
                .level0((int) level0)
                .level5((int) level5)
                .level10((int) level10)
                .level15((int) level15)
                .level20((int) level20)
                .level25((int) level25)
                .build();
        //历史告警详情
        List<XljHistoryDetail> xljHistoryDetails = xljWarningList.stream().map(warning -> {
            XljHistoryDetail xljHistoryDetail = new XljHistoryDetail();
            xljHistoryDetail.setWaterLevel(warning.getMaxDepth());
            xljHistoryDetail.setTime(warning.getMaxDepthTime());
            XljWarningFlow xljWarningFlow = warning.getXljWarningFlow();
            if (ObjectUtils.isEmpty(xljWarningFlow)) {
                return new XljHistoryDetail();
            }
            xljHistoryDetail.setMessageTime(xljWarningFlow.getMessageTime());
            xljHistoryDetail.setSendTraPoliceTime(xljWarningFlow.getSendTraPolice());
            xljHistoryDetail.setSendManCentTime(xljWarningFlow.getSendManCent());
            xljHistoryDetail.setSendWatServTime(xljWarningFlow.getSendWatServ());
            List<XljWarningFlow.XljStatus> statusList = xljWarningFlow.getXljStatusList();
            if (CollectionUtils.isNotEmpty(statusList)) {
                Optional<XljWarningFlow.XljStatus> warningStatus = statusList.stream().filter(status -> status.getXljCondition().equals(1)).findFirst();
                xljHistoryDetail.setWarningTime(warningStatus.map(XljWarningFlow.XljStatus::getCondTime).orElse(null));
                xljHistoryDetail.setWarningPicUrl(warningStatus.map(XljWarningFlow.XljStatus::getCondImgUrl).orElse(null));
                Optional<XljWarningFlow.XljStatus> closedStatus = statusList.stream().filter(status -> status.getXljCondition().equals(3)).findFirst();
                xljHistoryDetail.setClosedTime(closedStatus.map(XljWarningFlow.XljStatus::getCondTime).orElse(null));
                xljHistoryDetail.setClosedPicUrl(closedStatus.map(XljWarningFlow.XljStatus::getCondImgUrl).orElse(null));
                Optional<XljWarningFlow.XljStatus> unclosedStatus = statusList.stream().filter(status -> status.getXljCondition().equals(4)).findFirst();
                xljHistoryDetail.setUnClosedTime(unclosedStatus.map(XljWarningFlow.XljStatus::getCondTime).orElse(null));
                xljHistoryDetail.setUnClosedPicUrl(unclosedStatus.map(XljWarningFlow.XljStatus::getCondImgUrl).orElse(null));
                Optional<XljWarningFlow.XljStatus> finishStatus = statusList.stream().filter(status -> status.getXljCondition().equals(5)).findFirst();
                xljHistoryDetail.setFinishTime(finishStatus.map(XljWarningFlow.XljStatus::getCondTime).orElse(null));
                xljHistoryDetail.setFinishPicUrl(finishStatus.map(XljWarningFlow.XljStatus::getCondImgUrl).orElse(null));
            }
            return xljHistoryDetail;
        }).collect(Collectors.toList());
        return XljHistoryVO.builder()
                .lastModificationTime(xlj.getXljLatestInfo() == null ? xlj.getRcvTime() : xlj.getXljLatestInfo().getUpdateTime())
                .latitude(xlj.getLatitude())
                .longitude(xlj.getLongitude())
                .uniIdHuwan(uniIdHuwan)
                .warnings(warnings)
                .dailyLevel(warnings)
                .xljHistoryDetails(xljHistoryDetails)
                .statistics(xljStatisticGroup)
                .build();
    }

    public PageInfo<FallDownVO> queryFallDown(Integer start, Integer limit, String fallDownType) {
        PageInfo<FallDownVO> pageResult = new PageInfo<>();
        ArrayList<FallDownVO> fallDownVOS = new ArrayList<>();
        Query query = new Query();
        Query queryByType = query.addCriteria(Criteria.where("type").is(fallDownType));
        queryByType.with(new Sort(Sort.Direction.DESC, "first_hit_time"));
        List<Hit> treeHit = hitDao.findByQuery(queryByType);
        Map<String, List<Hit>> roadHitMap = new HashMap<>();
        treeHit.forEach(item -> {
            String roadName = item.getAttrs().getRoadName();
            String[] split = roadName.split(",");
            String road = split[0];
            List<Hit> roadHitList = roadHitMap.getOrDefault(road, new ArrayList<>());
            roadHitList.add(item);
            roadHitMap.put(road, roadHitList);
        });

        roadHitMap.forEach((k, v) -> {
            Map<String, List<Hit>> dateHitMap = new TreeMap<>();
            for (Hit hit : v) {
                Long hitTime = hit.getHitTime();
                String format = yMdSimpleDateFormat.format(new Date(hitTime * 1000));
                List<Hit> dateHitList = dateHitMap.getOrDefault(format, new ArrayList<>());
                dateHitList.add(hit);
                dateHitMap.put(format, dateHitList);
            }
            Hit hit = v.get(0);
            FallDownVO fallDownVO = new FallDownVO();
            fallDownVO.setRoadName(k);
            fallDownVO.setId(hit.getId());
            fallDownVO.setHandleCompany(hit.getAttrs().getHandleCompany());
            List<FallDownDetailVO> fallDownDetailVOList = new ArrayList<>();
            dateHitMap.forEach((key, value) -> {
                FallDownDetailVO fallDownDetailVO = new FallDownDetailVO();
                int sum = value.stream().mapToInt(Hit::getFallDownNumber).sum();
                fallDownDetailVO.setTime(value.get(0).getHitTime());
                fallDownDetailVO.setImage(value.get(0).getAttrs().getSceneImage());
                fallDownDetailVO.setFallDownNumber(sum);
                fallDownDetailVOList.add(fallDownDetailVO);
            });
            fallDownVO.setDetails(fallDownDetailVOList);
            fallDownVO.setFallDownNumber(fallDownDetailVOList.get(0).getFallDownNumber());
            int fixNumber = fallDownDetailVOList.get(0).getFallDownNumber() - fallDownDetailVOList.get(fallDownDetailVOList.size() - 1).getFallDownNumber();
            fallDownVO.setFixedNumber(Math.max(fixNumber, 0));
            fallDownVOS.add(fallDownVO);
        });
        pageResult.setTotal(fallDownVOS.size());
        if (fallDownVOS.size() > start) {
            int end = start + limit;
            if (start + limit > fallDownVOS.size()) {
                end = fallDownVOS.size();
            }
            List<FallDownVO> fallDownVOS1 = fallDownVOS.subList(start, end);
            pageResult.setList(fallDownVOS1);
        } else {
            pageResult.setList(new ArrayList<>());
        }
        return pageResult;
    }

    public int countXlj(Query query) {
        List<Xlj> xljList = xljDao.findByQuery(query);
        return xljList.size();
    }
}
